/**
 * 数据劫持的过程
 * 原理：采用Object.defineProperty()
 */
const user = {
    id:1,
    name:"xiaowang"
}

/**
 * 封装一个函数，专门用于劫持
 * @param {} data 
 * @param {*} key 
 * @param {*} value 
 */
function defineReactive(data,key,value){
    Object.defineProperty(data,key,{
        get(){
            console.log("进入get");
            return value
        },
        set(val){
            console.log("进入set",val);
            value = val
        }
    })
}

// defineReactive(user,"name",user.name)
// let key = "id"  user[key]
Object.keys(user).forEach(key=>{
    defineReactive(user,key,user[key])
})


console.log(user.name);
user.name = "xiaofeifei"
console.log(user);

console.log(user.id);
user.id = 2
console.log(user);


